import urls from '/util/urls'
import request from '/util/request'
import { parseTime, getDateBeginTimestamp, getDateEndTimestamp } from '/util/utils'
const app = getApp()

Page({
  data: {
    dailyWorks: [],
  },
  onLoad() {
    request.login(this.init.bind(this))
  },
  init() {
    const that = this
    const searchable = {
      s_beEqReportDate: getDateEndTimestamp(new Date()),
      s_afEqReportDate: getDateBeginTimestamp(new Date()),
      sort: 'serviceStartTime,desc',
    }
    request.get(urls.workDailyPageUrl(), searchable, function(data){
      that.setData({
        dailyWorks: data.map(it => {
          return {
            id: it.id,
            unitName: it.unitName,
            serviceStartTime: parseTime(it.serviceStartTime, '{h}:{i}'),
            serviceEndTime: parseTime(it.serviceEndTime, '{h}:{i}'),
          }
        })
      })
    })
  },
  onWrite(e) {
    dd.redirectTo({
      url: '/pages/writedaily/writedaily'
    })
  },
  onItemClick(e) {
    const id = e.index
    dd.redirectTo({
      url: '/pages/writedaily/writedaily?id=' + id
    })
  }
});
